package com.example.dllo.moondog.contact;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.easemob.chat.EMContactListener;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.example.dllo.moondog.R;
import com.example.dllo.moondog.base.BaseActivity;

import java.util.List;

/**
 * Created by dllo on 16/3/3.
 */
public class AddFriendActivity extends BaseActivity implements View.OnClickListener {
    private Button btnBack,btnFind;
    private EditText nameEditText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);
        btnBack = (Button) findViewById(R.id.btn_back);
        btnFind = (Button) findViewById(R.id.btn_find);
        nameEditText = (EditText) findViewById(R.id.et_find);

        EMChatOptions options = EMChatManager.getInstance().getChatOptions();
        options.setAcceptInvitationAlways(false);
        options.setUseRoster(true);
//        EMChat.getInstance().setAppInited();

        btnBack.setOnClickListener(this);
        btnFind.setOnClickListener(this);

        testMethod();
    }

    private void testMethod() {
        EMContactManager.getInstance().setContactListener(new EMContactListener() {

            @Override
            public void onContactAgreed(String username) {
                //好友请求被同意
                Log.d("AddFriendActivity", "好友请求被同意");
            }

            @Override
            public void onContactRefused(String username) {
                //好友请求被拒绝
                Log.d("AddFriendActivity", "好友请求被拒绝" + username);
            }

            @Override
            public void onContactInvited(String username, String reason) {
                //收到好友邀请
                Log.d("AddFriendActivity", "收到好友邀请" + username + "----" + reason);
            }

            @Override
            public void onContactDeleted(List<String> usernameList) {
                //被删除时回调此方法
                Log.d("AddFriendActivity", "被删除时回调此方法");
            }


            @Override
            public void onContactAdded(List<String> usernameList) {
                //增加了联系人时回调此方法
                Log.d("AddFriendActivity", "增加了联系人时回调此方法");
            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back :
                this.finish();
                break;
            case R.id.btn_find :
                findFriend();
                break;
        }
    }

    private void findFriend() {
        String username = nameEditText.getText().toString();
        if (EMChatManager.getInstance().getCurrentUser().equals(username)) {
            Toast.makeText(this, "不能添加自己", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            EMContactManager.getInstance().addContact(username, "请求加好友");
            Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show();

        } catch (EaseMobException e) {
            e.printStackTrace();
            Toast.makeText(this, "发送失败", Toast.LENGTH_SHORT).show();
        }


    }
}
